﻿#include "CDevCamer.h"
#include "CSqlOper.h"
#include "CVideoDevCfgOper.h"
#include "Public.h"
#include "CGlobal.h"
#include "CAlarmPoint.h"

CDevCamer::CDevCamer(QString sDevGuid, QString sDevName,bool& bRet)
{
	m_pCamerCtrMange = nullptr;
	m_sDevGuid = sDevGuid;
	m_sDevName = sDevName;
	bRet=init(sDevGuid);
}

CDevCamer::~CDevCamer()
{
	
	if (m_pVideoWorkMange != nullptr)
	{
		delete m_pVideoWorkMange;
		m_pVideoWorkMange = nullptr;
	}

	if (m_pCamerCtrMange != nullptr)
	{
		delete m_pCamerCtrMange;
		m_pCamerCtrMange = nullptr;
	}

	if (m_pDevData != nullptr)
	{
		delete m_pDevData;
		m_pDevData = nullptr;
	}
}

bool CDevCamer::ctrDev(SCtrInfo& stCtrInfo)
{
	return m_pCamerCtrMange->CtrDevice(stCtrInfo);
}

bool CDevCamer::init(QString sDevGuid)
{
	m_pDevData = new CDevDataCam();
	m_pDevData->m_sDevGuid = sDevGuid;
	//数据库中查询该摄像头配置信息
	{
		QString sErrMsg = "";
		QVector<QSqlRecord> vecRecord;
		bool bSql=CSqlOper::instance().selectCamerInfo(sDevGuid, vecRecord, sErrMsg);   //创建设备数据
		if (!bSql)
		{
			return false;
		}
		if (vecRecord.size() == 0)//没查到
		{
			CVideoDevCfgOper oper(sDevGuid);
			int nRet=oper.exec();
			if (!nRet == QDialog::Accepted)
			{
				return false;
			}
		}
	}

	QString sErrMsg = "";
	bool bRet=CSqlOper::instance().selectCamerInfo(sDevGuid, m_pDevData, sErrMsg);   //创建设备数据
	if (!bRet)
	{
		return false;
	}

	//初始化设备测点集合
	CAlarmPoint* pPointCamerState = new CAlarmPoint("VideoState", A2T("摄像头连接状态"), NULL, EPointType::enDigital, "");
	pPointCamerState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enVideoState, pPointCamerState);
	CAlarmPoint* pPointCoalFlowState = new CAlarmPoint("CoalFlowState", A2T("煤流量状态"), NULL, EPointType::enDigital, "");
	pPointCoalFlowState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enCoalFlowState, pPointCoalFlowState);
	CAlarmPoint* pPointBeltForeignState = new CAlarmPoint("BeltForeignState", A2T("皮带异物状态"), NULL, EPointType::enDigital, "");
	pPointBeltForeignState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enBeltForeignState, pPointBeltForeignState);
	CAlarmPoint* pPointLargeCoalState = new CAlarmPoint("LargeCoalState",A2T("大块煤状态"), NULL, EPointType::enDigital, "");
	pPointLargeCoalState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enLargeCoalState, pPointLargeCoalState);
	CAlarmPoint* pPointDeviationState = new CAlarmPoint("BeltDeviationState", A2T("皮带跑偏状态"), NULL, EPointType::enDigital, "");
	pPointDeviationState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enBeltDeviationState, pPointDeviationState);
	CAlarmPoint* pPointSmokeFireState = new CAlarmPoint("SmokeFireState", A2T("烟雾火点状态"), NULL, EPointType::enDigital, "");
	pPointSmokeFireState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enSmokeFireState, pPointSmokeFireState);
	CAlarmPoint* pPointSafetyHelmetState = new CAlarmPoint("SafetyHelmetState", A2T("安全帽反光衣状态"), NULL, EPointType::enDigital, "");
	pPointSafetyHelmetState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enSafetyHelmetState, pPointSafetyHelmetState);
	CAlarmPoint* pPointSmokeMakeCallState = new CAlarmPoint("SmokeMakeCallState", A2T("抽烟打电话状态"), NULL, EPointType::enDigital, "");
	pPointSmokeMakeCallState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enSmokeMakeCallState, pPointSmokeMakeCallState);
	CAlarmPoint* pPointTempState = new CAlarmPoint("Temp", A2T("测温报警状态"), NULL, EPointType::enDigital, "");
	pPointTempState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enTempState, pPointTempState);
	CAlarmPoint* pPointPerimeterState = new CAlarmPoint("Perimeter", A2T("周界防范状态"), NULL, EPointType::enDigital, "");
	pPointPerimeterState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enPerimeterState, pPointPerimeterState);
	CAlarmPoint* pPointBlockState = new CAlarmPoint("Block", A2T("传送带堵料状态"), NULL, EPointType::enDigital, "");
	pPointBlockState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enBlockState, pPointBlockState);
	CAlarmPoint* pPointThrowState = new CAlarmPoint("Throw", A2T("抛洒滴漏"), NULL, EPointType::enDigital, "");
	pPointThrowState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enThrowState, pPointThrowState);
	CAlarmPoint* pPointStreetState = new CAlarmPoint("Street", A2T("街面行为"), NULL, EPointType::enDigital, "");
	pPointStreetState->setAlarmValue(false);
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enStreetState, pPointStreetState);

	CMeasurePoint* pPointBeltRun = new CMeasurePoint("BeltRun", A2T("皮带启停信号"), "", nullptr, EPointType::enDigital, "");
	m_pDevData->m_mapDevPoint.insert(EDevPointType::enBeltRunState, pPointBeltRun);

	//创建摄像头控制线程
	m_pCamerCtrMange = new CCamerCtrMange(m_pDevData);
	//创建摄像头业务逻辑线程
	m_pVideoWorkMange = new CVideoWorkMange(m_pCamerCtrMange,m_pDevData);
	return true;
}
